/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package databook.controller;

import prefuse.util.ui.JRangeSlider;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author zach
 */
public class DBRangeSlider extends JPanel implements PropertyListener {
    JRangeSlider slider;
    JLabel startLabel;
    JLabel endLabel;
    
    public DBRangeSlider() {
        super();
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
        int num = Props.getInt(Props.NUMBER_OF_TERMS)-1;
        slider = new JRangeSlider(0,num,Props.getInt(Props.CURRENT_RANGE_MIN,0),Props.getInt(Props.CURRENT_RANGE_MAX,num),JRangeSlider.HORIZONTAL);
        startLabel = new JLabel(getLabel(0));
        endLabel = new JLabel(getLabel(num));
        add(startLabel);
        add(Box.createRigidArea(new Dimension(5,0)));
        add(slider);
        add(Box.createRigidArea(new Dimension(5,0)));
        add(endLabel);
        
        slider.addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent e) {
                    for (int i = 0; i <= slider.getMaximum(); i++) {
                        if (i < slider.getLowValue() || i > slider.getHighValue()) {
                            if (Props.filter("TERM",getTerm(i))) {
                                Props.setFilter("TERM",getTerm(i),false,false);
                            }
                        } else {
                            if (!Props.filter("TERM",getTerm(i))) {
                                Props.setFilter("TERM",getTerm(i),true,false);
                            }
                        }
                    }
                    Props.set(Props.CURRENT_RANGE_MIN,slider.getLowValue());
                    Props.set(Props.CURRENT_RANGE_MAX,slider.getHighValue());
                }
        });
        slider.addChangeListener(new javax.swing.event.ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    startLabel.setText(getLabel(slider.getLowValue()));
                    endLabel.setText(getLabel(slider.getHighValue()));
                }
        });
        
        Props.addListener(new String[] {Props.CURRENT_RANGE_MAX,Props.CURRENT_RANGE_MIN},this);
    }
    
    public static int[] translate(int num) {
        int sem = Integer.parseInt(Props.getString(Props.START_TERM).substring(5));
        int year = Props.getInt(Props.START_YEAR);
        if (num % 2 != 0) {
            if (sem == 1) 
                sem = 2;
            else {
                sem = 1;
                year++;
            }
            num--;
        }
        return new int[] {(year + (num/2)),sem};
        //return (year + (num/2)) + "-" + sem;
    }
    
    public static String getLabel(int num) {
        int[] tran = translate(num);
        return tran[0] + " " + (tran[1] == 1 ? "Sp" : "Fa");
    }
    
    public static String getTerm(int num) {
        int[] tran = translate(num);
        return tran[0] + "-" + tran[1];
    }
    
    public void propertyChanged(PropertyEvent evt) {
        if (evt.name.equals(Props.CURRENT_RANGE_MIN)) {
            slider.setLowValue(Props.getInt(evt.name));
        } else if (evt.name.equals(Props.CURRENT_RANGE_MAX)) {
            slider.setHighValue(Props.getInt(evt.name));
        }
    }
}
